<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\common\PublicController;
use Cookie;
use DB;

class IndexController extends PublicController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        // $agent_id = $request -> cookie('logon')['agent_id'];
        $logon = $request -> cookie('logon');
        $idstostr = Db::table('role')->where('id',$logon['role_id'])->value('area_ids');
        return view('admin.index.index',['agent_id'=>$logon['agent_id'],'ids'=>$idstostr]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getShow(Request $request)
    {
        $cookie = $request->cookie('logon');
        $areaidstr = DB::table('role') -> where('id',$cookie['role_id']) ->value('area_ids');
        $areaids = explode(',',$areaidstr);
        $type = $request->input('Types');
        if ($type) {
            if($cookie['agent_id'] == 1){
                if($areaidstr == 0){
                    $data = DB::table($type .' as dev')
                        -> leftJoin('room as r','dev.room_id','=','r.id')
                        -> leftJoin('building as b','r.building_id','=','b.id')
                        -> select('b.latitude as lat','b.longitude as long','dev.DevEUI')
                        -> get();
                }else{
                    $data = DB::table($type .' as dev')
                        -> leftJoin('room as r','dev.room_id','=','r.id')
                        -> leftJoin('building as b','r.building_id','=','b.id')
                        -> select('b.latitude as lat','b.longitude as long','dev.DevEUI')
                        -> whereIn('b.area_id',$areaids)
                        -> get();
                }
            }else{
                if($areaidstr == 0){
                    $data = DB::table($type .' as dev')
                        -> leftJoin('room as r','dev.room_id','=','r.id')
                        -> leftJoin('building as b','r.building_id','=','b.id')
                        -> select('b.latitude as lat','b.longitude as long','dev.DevEUI')
                        -> where('dev.agent_id',$cookie['agent_id'])
                        -> get();
                }else{
                    $data = DB::table($type .' as dev')
                        -> leftJoin('room as r','dev.room_id','=','r.id')
                        -> leftJoin('building as b','r.building_id','=','b.id')
                        -> select('b.latitude as lat','b.longitude as long','dev.DevEUI')
                        -> where('dev.agent_id',$cookie['agent_id'])
                        -> whereIn('b.area_id',$areaids)
                        -> get();
                }
            }
            /*foreach ($data as $key=>$val){
                $areaMember = '';
                $buildingMember = '';
                $roomMember = '';
                $areaMemberArr = $this -> memberInRange('area',$data[$key]['a_id']);
                $buildingMemberArr = $this -> memberInRange('building',$data[$key]['b_id']);
                $roomMemberArr = $this -> memberInRange('room',$val['r_id']);
                if(!empty($areaMemberArr)){
                    foreach($areaMemberArr as $v){
                        $areaMember .= $v['name'].'-'.$v['phone'].',';
                    }
                }
                if(!empty($areaMemberArr)) {
                    foreach ($buildingMemberArr as $v) {
                        $buildingMember .= $v['name'].'-'.$v['phone'].',';
                    }
                }
                if(!empty($roomMemberArr)) {
                    foreach ($roomMemberArr as $v) {
                        $roomMember .= $v['name'].'-'.$v['phone'].',';
                    }
                }
                $data[$key]['member'] = array(
                    'area'=>substr($areaMember,0,-1),
                    'building'=>substr($buildingMember,0,-1),
                    'room'=>substr($roomMember,0,-1)
                );
            }*/
        }else{
            $data=NUll;
        }
        return $data;
    }

    public function getInfo(Request $request){
            $type = $request->input('Types');
            $DevEUI = $request->input('DevEUI');
            $data = DB::table($type.' as dev')
                    -> leftJoin('room as r','dev.room_id','=','r.id')
                    -> leftJoin('building as b','r.building_id','=','b.id')
                    -> leftJoin('area as a','b.area_id','=','a.id')
                    -> leftJoin('model as m','m.id','=','dev.model_id')
                    -> leftJoin('type as t','t.id','=','m.type_id')
                    -> where('DevEUI',$DevEUI)
                    -> select('DevEUI','t.name as tname','a.id as a_id','a.name as aname','b.id as b_id','b.name as bname','r.id as r_id','r.name as rname')
                    ->first();
                $areaMember = '';
                $buildingMember = '';
                $roomMember = '';
                $areaMemberArr = $this -> memberInRange('area',$data['a_id']);
                $buildingMemberArr = $this -> memberInRange('building',$data['b_id']);
                $roomMemberArr = $this -> memberInRange('room',$data['r_id']);
                if(!empty($areaMemberArr)){
                    foreach($areaMemberArr as $v){
                        $areaMember .= $v['name'].'-'.$v['phone'].',';
                    }
                }
                if(!empty($areaMemberArr)) {
                    foreach ($buildingMemberArr as $v) {
                        $buildingMember .= $v['name'].'-'.$v['phone'].',';
                    }
                }
                if(!empty($roomMemberArr)) {
                    foreach ($roomMemberArr as $v) {
                        $roomMember .= $v['name'].'-'.$v['phone'].',';
                    }
                }
                $data['member'] = array(
                    'area'=>$areaMember?substr($areaMember,0,-1):'',
                    'building'=>$buildingMember?substr($buildingMember,0,-1):'',
                    'room'=>$roomMember?substr($roomMember,0,-1):''
                );
                return json_encode($data);
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
